#include <QDoubleValidator>
#include "biblereadercalculatordlg.h"

/**
 * @brief BibleReaderCalculatorDlg::BibleReaderCalculatorDlg
 * @param parent
 */
BibleReaderCalculatorDlg::BibleReaderCalculatorDlg(QWidget *parent):
    QDialog (parent)
{
    buildBibleUnits();
    createWidgets();
}

BibleReaderCalculatorDlg::~BibleReaderCalculatorDlg()
{
    if (fromField) {
        delete fromField;
    }

    if (fromUnit) {
        delete fromUnit;
    }

    if (approximate) {
        delete approximate;
    }

    if (toField) {
        delete toField;
    }
}

void BibleReaderCalculatorDlg::createWidgets()
{
    // set title
    setWindowTitle(tr("Biblical Metrics Converter"));
    setMinimumWidth(640);
    fromField = new QLineEdit(this);
    fromField->setValidator(new QDoubleValidator(0.0, 100000000.0, 2, this));
    connect(fromField, &QLineEdit::textChanged, this, &BibleReaderCalculatorDlg::calculate);

    fromUnit = new QComboBox(this);
    // add bible units to box
    for (int i = 0; i < units.size(); i++) {
        fromUnit->addItem(units[i].getBibleUnitName(), units[i].getType());
    }
    connect(fromUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(calculate()));

    approximate = new QLabel(this);
    approximate->setText(tr("~"));

    toField = new QLabel(this);
    toField->setTextFormat(Qt::RichText);

    layout = new QHBoxLayout(this);

    layout->addWidget(fromField, 3);
    layout->addWidget(fromUnit, 3);
    layout->addWidget(approximate, 1);
    layout->addWidget(toField, 3);

    setLayout(layout);
}

void BibleReaderCalculatorDlg::buildBibleUnits()
{
    // clear cache
    units.clear();

    // building one by one...
    // weight
    units.append(BibleUnit(TALENT, tr("TALENT"), tr("KG"), 34));// 他连得
    units.append(BibleUnit(MINA, tr("MINA"), tr("KG"), 0.6));// 弥拿
    units.append(BibleUnit(SHEKEL, tr("SHEKEL"), tr("G"), 11.5));// 舍客勒
    units.append(BibleUnit(BEKA, tr("BEKA"), tr("G"), 5.5));// 比加
    units.append(BibleUnit(GERAH, tr("GERAH"), tr("G"), 0.6));// 季拉

    // length
    units.append(BibleUnit(QANEH, tr("QANEH"), tr("CM"), 267));// 竿
    units.append(BibleUnit(AMMAH, tr("AMMAH"), tr("CM"), 44.5));// 肘（旧约）
    units.append(BibleUnit(ZERETH, tr("ZERETH"), tr("CM"), 22.3));// 虎口
    units.append(BibleUnit(TOPHACH, tr("TOPHACH"), tr("CM"), 7.4));// 掌
    units.append(BibleUnit(ETSBA, tr("ETSBA"), tr("CM"), 1.85));// 指

    // Rong Liang
    units.append(BibleUnit(HOMER, tr("HOMER"), tr("L"), 220));// 贺梅珥
    units.append(BibleUnit(COR, tr("COR"), tr("L"), 220));// 歌珥/柯珥
    units.append(BibleUnit(LETHEH, tr("LETHEH"), tr("L"), 110));// 利特克
    units.append(BibleUnit(EPHAH, tr("EPHAH"), tr("L"), 22));// 伊法
    units.append(BibleUnit(SEAH, tr("SEAH"), tr("L"), 7.3));// 细亚
    units.append(BibleUnit(OMER, tr("OMER"), tr("L"), 2.4));// 俄梅珥
    units.append(BibleUnit(KAB, tr("KAB"), tr("L"), 1.2));// 升
    units.append(BibleUnit(BATH, tr("BATH"), tr("L"), 22));// 罢特
    units.append(BibleUnit(HIN, tr("HIN"), tr("L"), 3.6));// 欣
    units.append(BibleUnit(LOG, tr("LOG"), tr("L"), 0.3));// 罗革

    // in New Testament
    units.append(BibleUnit(LITRA, tr("LITRA"), tr("KG"), 0.34));// 斤
    units.append(BibleUnit(MILION, tr("MILION"), tr("M"), 1478));// 里
    units.append(BibleUnit(ORGUIA, tr("ORGUIA"), tr("CM"), 185));// 丈
    units.append(BibleUnit(PECHYS, tr("PECHYS"), tr("CM"), 45));// 肘（新约）
    units.append(BibleUnit(KOROS, tr("KOROS"), tr("L"), 350));// 石
    units.append(BibleUnit(SATON, tr("SATON"), tr("L"), 7.3));// 斗 同旧约的 细亚
    units.append(BibleUnit(CHOINIX, tr("CHOINIX"), tr("L"), 1.1));// 升
    units.append(BibleUnit(METRETES, tr("METRETES"), tr("L"), 39));// 桶
    units.append(BibleUnit(BATOS, tr("BATOS"), tr("L"), 22));// 篓， 同旧约的 罢特
}

void BibleReaderCalculatorDlg::calculate()
{
    QString value = fromField->text();
    double result = 0.0;

    for (int i=0; i <= units.size(); i++) {
        if ( fromUnit->currentData().toInt() == units[i].getType() ) {
            result = value.toDouble() * units[i].getRatio();
            toField->setText(tr("%1 <font color='blue'>%2</font>").arg(result).arg(units[i].getNowUnitName()));
            break;
        }
    }
}
